﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;
using HotelManagement;

namespace HotelManagement._01.FORM
{
    public partial class F25_CheckOut : Form
    {
        public F25_CheckOut()
        {
            InitializeComponent();
            Init();
            LoadDataToGrid();
        }

        int m_Cost;
        string m_RoomID;
        CheckOut m_CheckOut;
        CheckOutBUS m_CheckOutBUS;
        RoomStatusBUS m_RoomStatusBUS;

        private void Init()
        {
            m_CheckOut = new CheckOut();
            m_CheckOutBUS = new CheckOutBUS();
            m_RoomStatusBUS = new RoomStatusBUS();
        }
        private void LoadDataToGrid()
        {
            m_dgv_CheckOut.DataSource = m_CheckOutBUS.ViewCheckOut();
            //m_cbo_RoomID.DataSource = m_CheckOutBUS.SelectRoomID();
            //m_cbo_RoomID.DisplayMember = "RoomID";
            //m_cbo_RoomID.ValueMember = "RoomID";
        }

        private void m_dgv_CheckOut_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_CheckOut.CheckInID = int.Parse(m_dgv_CheckOut.Rows[m_dgv_CheckOut.CurrentCell.RowIndex].Cells["CheckInID"].Value.ToString());
                m_CheckOut.CheckInDate = DateTime.Parse(m_dgv_CheckOut.Rows[m_dgv_CheckOut.CurrentCell.RowIndex].Cells["CheckInDate"].Value.ToString());
                m_CheckOut.CustomerName = m_dgv_CheckOut.Rows[m_dgv_CheckOut.CurrentCell.RowIndex].Cells["CustomerName"].Value.ToString();
                m_CheckOut.RoomID = m_dgv_CheckOut.Rows[m_dgv_CheckOut.CurrentCell.RowIndex].Cells["RoomID"].Value.ToString();
                m_CheckOut.Note = m_dgv_CheckOut.Rows[m_dgv_CheckOut.CurrentCell.RowIndex].Cells["Note"].Value.ToString();
                m_CheckOut.Fines = 0;
                m_CheckOut.CheckOutID = 0;

                DataTable v_dt = m_CheckOutBUS.ViewInfoCustomer(m_CheckOut.CheckInID);

                m_txt_CustomerName.Text = v_dt.Rows[0][1].ToString();
                m_txt_IDCardNo.Text = v_dt.Rows[0][8].ToString();
                m_txt_Phone.Text = v_dt.Rows[0][6].ToString();
                m_txt_PassPort.Text = v_dt.Rows[0][9].ToString();
                m_txt_Address.Text = v_dt.Rows[0][5].ToString();

                m_time_CheckInDate.Text = m_CheckOut.CheckInDate.ToString();
                m_txt_Note.Text = m_CheckOut.Note;
                m_txt_RoomID.Text = m_CheckOut.RoomID.ToString();

                DataTable v_dtCost = m_CheckOutBUS.GetCostByRoomID(m_CheckOut.RoomID);
                m_Cost = int.Parse(v_dtCost.Rows[0][0].ToString());
                DateTime v_begin = m_time_CheckInDate.Value;
                DateTime v_end = m_time_CheckOutDate.Value;

                int v_SoNgayThue = CalculatorRentCost(v_begin, v_end);
                m_CheckOut.RentCost = v_SoNgayThue * m_Cost;

                m_txt_RentCost.Text = m_CheckOut.RentCost.ToString();


            }
            catch (Exception ex)
            {

                MessageBox.Show("Bạn chưa chọn loại dịch vụ nào !");
            }
        }

        private void m_btn_Search_Click(object sender, EventArgs e)
        {
            string v_CustomerName = m_txt_Search.Text.ToString();
            m_dgv_CheckOut.DataSource = m_CheckOutBUS.ViewSearchByCustomerName(v_CustomerName);
        }

        private void m_time_CheckOutDate_ValueChanged(object sender, EventArgs e)
        {
            DateTime v_begin = m_time_CheckInDate.Value;
            DateTime v_end = m_time_CheckOutDate.Value;
            int v_SoNgayThue = CalculatorRentCost(v_begin, v_end);
            m_CheckOut.RentCost = v_SoNgayThue * m_Cost;

            m_txt_RentCost.Text = m_CheckOut.RentCost.ToString();
        }


        protected int CalculatorRentCost(DateTime ip_begin, DateTime ip_end)
        {
            int v_Year = ip_begin.Year - ip_end.Year;
            return v_Year * 365 + ip_end.DayOfYear - ip_begin.DayOfYear + 1;
        }

        private void m_btn_CheckOut_Click(object sender, EventArgs e)
        {
            if (m_CheckOut.CheckInID == 0)
            {
                MessageBox.Show("Bạn chưa chọn phòng nào!");
            }
            else
            {
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn trả phòng cho phòng đã chọn ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {
                    m_CheckOut.Note = "UnPaid";
                    m_CheckOut.CheckOutDate = m_time_CheckOutDate.Value;

                    DataTable v_dt = m_RoomStatusBUS.ViewOnlyRoom(m_CheckOut.RoomID);
                    if (v_dt.Rows.Count > 0)
                    {
                        m_CheckOut.Status = "Booked";
                    }
                    else
                    {
                        m_CheckOut.Status = "Empty";
                    }
                    if (m_CheckOutBUS.CheckOut(m_CheckOut))
                    {
                        MessageBox.Show("Thành công !");
                        LoadDataToGrid();
                    }
                    else MessageBox.Show("Có lỗi! Không cập nhật được.");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void m_ckb_Change_CheckedChanged(object sender, EventArgs e)
        {
            if (m_ckb_Change.Checked && m_txt_Fines.Text == "")
            {
                MessageBox.Show("Hãy nhập tiền phạt!");
                m_ckb_Change.Checked = false;
                
                
            }
            else if(m_ckb_Change.Checked && m_txt_Fines.Text != "")
            {
                m_CheckOut.Fines = int.Parse(m_txt_Fines.Text.ToString().Trim());
                m_CheckOut.CheckOutID = 1;
            }
            else
            {
                m_CheckOut.Fines = 0;
                m_CheckOut.CheckOutID = 0;
            }
        }

    }
}
